package www.tvbigo.cn.xiaoxuan.app;

import java.util.ArrayList;
import java.util.List;

import www.tvbigo.cn.xiaoxuan.R;
import www.tvbigo.cn.xiaoxuan.activity.HomeActivity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.animation.AccelerateInterpolator;
import android.widget.TextView;

public class APPActivity extends Activity implements ViewPager.OnPageChangeListener
{
    private List<AppBean> mAppList = null;
    
    // 一共的页数
    private int mPagerCount = -1;
    
    private List<AllApp> mPagerListAllApp = new ArrayList<AllApp>();
    
    private ViewPager mViewPager = null;
    
    private TextView pointer = null;
    
    private Rotate3dAnimation rotation;
    
    private Receiver receiver;
    
    private DataPagerAdapter<AllApp> adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.fragment_app);
        mViewPager = (ViewPager)findViewById(R.id.app_view_pager);
        pointer = (TextView)findViewById(R.id.app_pointer);
        initAnimation();
        pointer.startAnimation(rotation);
        initAllApp();
        mViewPager.setOnPageChangeListener(this);
    }
    
    /**
     * 3D旋转动画
     */
    private void initAnimation()
    {
        rotation = new Rotate3dAnimation(0, 360, 25, 25, 0.0f, false);
        rotation.setDuration(700);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator(2.0f));
    }
    
    public void setUserVisibleHint(boolean isVisibleToUser)
    {
        setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser)
        {
            if (pointer != null)
            {
                pointer.startAnimation(rotation);
            }
        }
        else
        {
            // ...
        }
    }
    
    /**
     * 初始化app数据和布局
     */
    public void initAllApp()
    {
        
        GetAppList getAppInstance = new GetAppList(getApplicationContext());
        mAppList = getAppInstance.getLaunchAppList();
        if (mPagerListAllApp != null && mPagerListAllApp.size() > 0)
        {
            mPagerListAllApp.clear();
        }
        if (mAppList.size() % 15 == 0)
        {
            mPagerCount = mAppList.size() / 15;
        }
        else
        {
            mPagerCount = mAppList.size() / 15 + 1;
        }
        
        for (int i = 0; i < mPagerCount; i++)
        {
            AllApp mAllayout = new AllApp(getApplicationContext());
            mAllayout.setAppList(mAppList, i, mPagerCount);
            mAllayout.managerAppInit();
            mPagerListAllApp.add(mAllayout);
        }
        
        adapter = new DataPagerAdapter<AllApp>(getApplicationContext(), mPagerListAllApp);
        mViewPager.setAdapter(adapter);
    }
    
    @Override
    public void onResume()
    {
        super.onResume();
    }
    
    @Override
    public void onStart()
    {
        super.onStart();
        receiver = new Receiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.PACKAGE_ADDED");
        filter.addAction("android.intent.action.PACKAGE_REMOVED");
        filter.addDataScheme("package");
        getApplicationContext().registerReceiver(receiver, filter);
    }
    
    @Override
    public void onPause()
    {
        super.onPause();
    }
    
    @Override
    public void onStop()
    {
        super.onStop();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        int code = event.getKeyCode();
        switch (code)
        {
            case KeyEvent.KEYCODE_BACK:
                Intent i = new Intent(APPActivity.this, HomeActivity.class);
                startActivity(i);
                APPActivity.this.finish();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (receiver != null)
        {
            getApplicationContext().unregisterReceiver(receiver);
        }
    }
    
    private class Receiver extends BroadcastReceiver
    {
        
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // 安装广播
            if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED"))
            {
                initAllApp();
            }
            // 卸载广播
            if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED"))
            {
                initAllApp();
            }
        }
    }
    
    @Override
    public void onPageScrollStateChanged(int arg0)
    {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2)
    {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void onPageSelected(int position)
    {
        // TODO Auto-generated method stub
        pointer.startAnimation(rotation);
        pointer.setText((position + 1) + "/" + mPagerCount);
    }
}
